


let layerIndex;
//弹出学生新增/更新对话框
function showStudentDlg(id) {
    let title = "新增学生";
    if (id) {
        //是编辑，将标题修改为“编辑学生”
        title = "编辑学生";
        $.ajax({
            url: "api/student/" + id,
            method: "GET"
        }).done(result => {
            console.log(result); // 调试
            // 确保 result 是一个对象，并且字段正确
            for (let key in result) {
                var field = $('#studForm [name="' + key + '"]');
                if (field.is(':radio')) {
                    field.filter('[value="' + result[key] + '"]').prop('checked', true);
                } else if (field.is(':checkbox')) {
                    field.prop('checked', result[key] === "yes");
                } else {
                    field.val(result[key]);
                }
            }
        });
    } else {
        //是新增
        $("#studForm")[0].reset();
        $("#formId").css("display", "none");
    }

    layerIndex = layer.open({
        type: 1,
        area: ['520px', 'auto'],
        title: title,
        content: $('#studForm')
    });
}

function getSearchCondtion(){

    let formData={}
    $('#queryForm').find('input,select').each(function(){
        let name=$(this).attr('name');
        let value=$(this).val();

        if (name && value){
            formData[name]=value;
        }
    });
    return formData
}


layui.use(function () {
    //(1) 验证表单是否合法
    layui.form.on('submit(stud-dlg)', function (data) {
        event.preventDefault(); // 阻止表单默认提交
        commitStuDlg();
    });

    //(2)表格初始化

    const table = layui.table;
    let student = getSearchCondtion();

    //创建渲染实例
    table.render({
        elem: '#tbStudent',
        url: '/api/student/getbypage',// 出处分静态模拟数银，死际使用时業搞成真实按日
        method: "POST",
        contentType: 'application/json ', // 确保以 JSON 格式发送
        where: {"data": student},
        page: true,
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'id', fixed: 'left', width: 80, title: 'id', sort: true},
            {field: 'name', title: '姓名'},

            {
                field: 'sno',
                title: '学号',
                width: 150,
            },
            {
                filed: 'sex', width: 80, title: '性别', sort: true,
                templet: d => d.sex === 1 ? '男' : (d.sex === 2 ? '女' : '未知'),
            },
            {field: 'age', width: 100, title: '年龄', sort: true},
            {fixed: 'right', title: '操作', width: 134, minWidth: 125, templet: '#editTemplate'}
        ]],
        done: function (rs) {
            //console.log（rs）
        }


    });

    //触发单元格工具事件
    // table.on('tool(tbStudent)', function (obj) {
    //     var data = obj.data; // 获得当前行数据
    //     //console.log(obj)
    //     if(obj.event === 'edit') {
    //         layer.open({
    //             title: '编辑 - id:' + data.id,
    //             type: 1,
    //             area: ['80%', '80%'],
    //             content: '<div style="padding:16px;">自定义表单元素</div>'
    //         });
    //     }
    // });
});

function search() {
    let student = getSearchCondtion();
    const table = layui.table;
    table.reloadData("tbStudent", {
        where: {data: student}
    });
    console.log("where condition:" + JSON.stringify(student))
}

function deleteConfirm() {
    const table = layui.table;
    //获取表格的选中状态
    const checkStatus = table.checkStatus('tbStudent'); // 'tbStudentIDlay-filter
    console.log(checkStatus)
}

function commitStuDlg() {
    let id = $("#id").val();
    let formData = $("#studForm").serialize();
    if (id!= null && id != "") {
        //是更新学生(1)!

        $.ajax({
            url: "/api/student/update",
            method: "PUT",
            data: formData
        }).done(result => {
            console.log(result);
            if (result.id) {
                //(4）读取并刷新原来的读学生列表
                loadStudentList();
                //(3）关闭弹出层
                console.log("add success!")
                if (layerIndex)
                    layer.close(layerIndex)
            }
        }).fail((jqXHR, textStatus, errorThrown) => {
            console.error("Request failed: " + textStatus + " - " + errorThrown);
            //可以在这里处理错误逻辑
            alert("An error occurred. Please try again.");
        })
    }
    else {
        //是新增学生(2)!
        //(2）将表单数据发送到服务器的insert中，把提交按钮变灰

        $.ajax({
            url: "/api/student/add",
            method: "POST",
            data: formData
        }).done(result => {
            console.log(result);
            if (result.id) {
                //(4）读取并刷新原来的读学生列表
                loadStudentList();
                //(3）关闭弹出层
                console.log("add success!")
                if (layerIndex)
                    layer.close(layerIndex)
            }
        }).fail((jqXHR, textStatus, errorThrown) => {
            console.error("Request failed: " + textStatus + " - " + errorThrown);
            //可以在这里处理错误逻辑
            alert("An error occurred. Please try again.");
        })
    }

    $("#btnOK").prop("disabled", true).addClass("layui-btn-disabled"); //禁用按钮
}
function deleteById(id) {
    //删除
    layer.confirm('你真的要删除吗？一旦删除，不可恢复', {icon: 3}, function () {
        $.ajax({
            url: "/api/student/delete/" + id,
            method: "DELETE"
        }).done(result => {
            loadStudentList();
        })
        layer.closeALL();//关闭所有层
    }, function () {

    });
}